<template>
  <div class="login-container">
    <!-- 背景装饰 -->
    <div class="background-decoration">
      <div class="circle circle-1"></div>
      <div class="circle circle-2"></div>
      <div class="circle circle-3"></div>
    </div>
    
    <!-- 登录卡片 -->
    <div class="login-card">
      <div class="login-header">
        <div class="logo-container">
          <div class="logo-icon">
            <svg viewBox="0 0 24 24" fill="currentColor">
              <path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm-2 15l-5-5 1.41-1.41L10 14.17l7.59-7.59L19 8l-9 9z"/>
            </svg>
          </div>
        </div>
        <h1 class="login-title">欢迎登录</h1>
        <p class="login-subtitle">请输入您的账号和密码</p>
      </div>
      
      <el-form
        ref="ruleFormRef"
        class="login-form"
        :model="ruleForm"
        :rules="rules"
        label-width="auto"
      >
        <el-form-item prop="UserName" class="form-item">
          <el-input 
            v-model="ruleForm.UserName" 
            placeholder="请输入账号"
            class="custom-input"
            size="large"
          >
            <template #prefix>
              <el-icon class="input-icon"><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        
        <el-form-item prop="UserPwd" class="form-item">
          <el-input 
            v-model="ruleForm.UserPwd" 
            type="password"
            placeholder="请输入密码"
            class="custom-input"
            size="large"
            show-password
          >
            <template #prefix>
              <el-icon class="input-icon"><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        
        <el-form-item class="form-item">
          <el-button 
            type="primary" 
            @click="submitForm(ruleFormRef)"
            class="login-button"
            size="large"
            :loading="loading"
          >
            {{ loading ? '登录中...' : '登录' }}
          </el-button>
        </el-form-item>
        
        <el-form-item class="form-item">
          <el-button 
            @click="resetForm(ruleFormRef)"
            class="reset-button"
            size="large"
          >
            重置
          </el-button>
        </el-form-item>
      </el-form>
      
      <div class="login-footer">
        <p class="footer-text">© 2024 库存管理系统. All rights reserved.</p>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import axios from 'axios'
import { useRouter } from 'vue-router'
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'

const router = useRouter();
import {useCounterStore} from '@/stores/counter'
const pinia=useCounterStore();
import { Read,Write } from '@/http/interceptors'

// 添加loading状态
const loading = ref(false)




const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive({
  UserName: '',
  UserPwd: '',
})

const locationOptions = ['Home', 'Company', 'School']

const rules = reactive<FormRules>({
  UserName: [
    { required: true, message: '账号不能为空', trigger: 'blur' },
  ],
  UserPwd: [
    { required: true, message: '密码不能为空', trigger: 'blur' },
  ],
})

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      loading.value = true
      console.log('submit!')
      Read.get("/api/LoginUserLogin",{params:ruleForm}).then(res=>{
        if(res.data.code==200)
        {
            localStorage.setItem('token',res.data.data.token);
            console.log('token:',localStorage.getItem('token'))
            pinia.account.UserId=res.data.data.userId
            pinia.account.UserName=res.data.data.userName
            pinia.account.RoleId=res.data.data.roleId
            pinia.account.RoleName=res.data.data.roleName
            pinia.account.Token=res.data.data.token
            ElMessage.success(res.data.message)
            router.push('/')
        }
        else{
            ElMessage.error(res.data.message)
        }
      }).finally(() => {
        loading.value = false
      })
    } else {
      console.log('error submit!', fields)
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}

</script>

<style scoped>
.login-container {
  min-height: 100vh;
  height: 100vh;
  width: 100vw;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  position: relative;
  overflow: hidden;
  margin: 0;
  padding: 0;
}

.background-decoration {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.circle {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  animation: float 6s ease-in-out infinite;
}

.circle-1 {
  width: 200px;
  height: 200px;
  top: 10%;
  left: 10%;
  animation-delay: 0s;
}

.circle-2 {
  width: 150px;
  height: 150px;
  top: 60%;
  right: 10%;
  animation-delay: 2s;
}

.circle-3 {
  width: 100px;
  height: 100px;
  bottom: 20%;
  left: 20%;
  animation-delay: 4s;
}

@keyframes float {
  0%, 100% {
    transform: translateY(0px);
  }
  50% {
    transform: translateY(-20px);
  }
}

.login-card {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-radius: 20px;
  padding: 40px;
  width: 100%;
  max-width: 420px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  position: relative;
  z-index: 1;
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.logo-container {
  margin-bottom: 20px;
}

.logo-icon {
  width: 60px;
  height: 60px;
  margin: 0 auto;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 24px;
}

.logo-icon svg {
  width: 32px;
  height: 32px;
}

.login-title {
  font-size: 28px;
  font-weight: 600;
  color: #333;
  margin: 0 0 8px 0;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.login-subtitle {
  font-size: 14px;
  color: #666;
  margin: 0;
}

.login-form {
  margin-bottom: 20px;
}

.form-item {
  margin-bottom: 20px;
}

.form-item :deep(.el-form-item__content) {
  width: 100%;
}

.custom-input {
  width: 100%;
}

.custom-input :deep(.el-input__wrapper) {
  background: rgba(255, 255, 255, 0.8);
  border: 2px solid transparent;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
}

.custom-input :deep(.el-input__wrapper:hover) {
  border-color: #667eea;
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.2);
}

.custom-input :deep(.el-input__wrapper.is-focus) {
  border-color: #667eea;
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
}

.input-icon {
  color: #667eea;
  font-size: 18px;
}

.login-button {
  width: 100%;
  height: 48px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  color: white;
  transition: all 0.3s ease;
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
}

.login-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
}

.login-button:active {
  transform: translateY(0);
}

.reset-button {
  width: 100%;
  height: 48px;
  background: rgba(255, 255, 255, 0.8);
  border: 2px solid #e0e0e0;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  color: #666;
  transition: all 0.3s ease;
}

.reset-button:hover {
  background: #f5f5f5;
  border-color: #d0d0d0;
  transform: translateY(-1px);
}

.login-footer {
  text-align: center;
  margin-top: 30px;
  padding-top: 20px;
  border-top: 1px solid rgba(0, 0, 0, 0.1);
}

.footer-text {
  font-size: 12px;
  color: #999;
  margin: 0;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .login-container {
    height: 100vh;
    width: 100vw;
    margin: 0;
    padding: 0;
  }
  
  .login-card {
    margin: 20px;
    padding: 30px 20px;
    max-width: calc(100vw - 40px);
  }
  
  .login-title {
    font-size: 24px;
  }
  
  .logo-icon {
    width: 50px;
    height: 50px;
  }
  
  .logo-icon svg {
    width: 28px;
    height: 28px;
  }
}

/* 深色模式支持 */
@media (prefers-color-scheme: dark) {
  .login-card {
    background: rgba(30, 30, 30, 0.95);
    border-color: rgba(255, 255, 255, 0.1);
  }
  
  .login-title {
    color: #fff;
  }
  
  .login-subtitle {
    color: #ccc;
  }
  
  .custom-input :deep(.el-input__wrapper) {
    background: rgba(50, 50, 50, 0.8);
    color: #fff;
  }
  
  .reset-button {
    background: rgba(50, 50, 50, 0.8);
    border-color: #444;
    color: #ccc;
  }
  
  .reset-button:hover {
    background: rgba(60, 60, 60, 0.8);
  }
  
  .footer-text {
    color: #888;
  }
}
</style>
